public class code2 {
    public int minFallingPathSum(int[][] matrix) {
        int m = matrix.length;

        int[][] dp =new int[m+1][m+2];//多开一行，多开两列

        //初始化：全部初始化为正无穷，然后第m一行为0
        for (int i = 1; i < m+1; i++) {
            dp[i][0] = dp[i][m+1] = Integer.MAX_VALUE;

        }

        for (int i = 1; i < m+1; i++) {
            for (int j = 1; j < m+1; j++) {
                dp[i][j] = Math.min(Math.min(dp[i-1][j-1],dp[i-1][j]),dp[i-1][j+1])+matrix[i-1][j-1];//注意下标的映射
            }
        }

        int min = Integer.MAX_VALUE;
        //返回值为最后一个的最小值
        for (int i = 1; i < m+1; i++) {
            min = Math.min(dp[m][i],min);
        }
        return min;
    }
}
